package com.yanqu.road.server.channelserver.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.entername.*;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameMyRankData;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRank;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRankModel;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.LinkedList;

@Cmd(code = ChannelProtocol.N_ENTER_NAME_USER_RANK, desc = "个人排行")
public class ChannelEnterNameUserRankCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        EnterNameProto.ChannelEnterNameUserRankReqMsg reqMsg = EnterNameProto.ChannelEnterNameUserRankReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int type = 0;
        if(reqMsg.hasType()){
            type = reqMsg.getType();
        }
        boolean containData = true;
        if(reqMsg.hasNoData() && reqMsg.getNoData()){
             containData = false;
        }
        if(type == 0){
            EnterNameActivity activity = ChannelEnterNameMgr.getEnterNameActivity(activityId);
            if(activity != null){
                //由具体活动决定
                type = activity.getUserConditionType();
            }
        }
        if(type == 0){
            //默认牧场点兵
            type = eGamePlayerEventType.EnterNameAreaFightRank.getValue();
        }
        long userId = reqMsg.getUserKey().getUserId();
        EnterNameProto.EnterNameUserRankRespMsg.Builder clientMsg = EnterNameProto.EnterNameUserRankRespMsg.newBuilder();
        clientMsg.setRet(0);
        clientMsg.setPage(reqMsg.getPage());
        clientMsg.setExtendParam("0");
        clientMsg.setMyParam("0");
        clientMsg.setMyValue("0");
        clientMsg.setMyRank(-1);
        EnterNameModel model = ChannelEnterNameMgr.getEnterNameModelMap().get(activityId);
        if(model != null) {
            //拿分组信息
            if(reqMsg.hasUserId()){
                userId = reqMsg.getUserId();
            }
            UserEnterName userEnterName = model.getUserEnterName(userId);
            if(userEnterName != null){
                clientMsg.setExtendParam(String.valueOf(userEnterName.getZoneId()));
                ActivityGroupMatch groupMatch = model.getActivityGroupMatch(userEnterName.getGroupId());
                if(groupMatch != null){
                    EnterNameRankModel enterNameRankModel = ChannelEnterNameRankMgr.getEnterNameRankModel(activityId, type, groupMatch.getRankMatchGroupId());
                    if(enterNameRankModel != null){
                        int page = reqMsg.getPage();
                        if(containData) {
                            int pageSize = reqMsg.getPageSize();
                            int start = (page - 1) * pageSize;
                            int end = page * pageSize;
                            LinkedList<EnterNameRank> list = enterNameRankModel.getRankList();
                            int size = list.size();
                            EnterNameProto.EnterNameUserRankListMsg.Builder listMsg = EnterNameProto.EnterNameUserRankListMsg.newBuilder();
                            for (int i = start; i < size && i < end; i++) {
                                EnterNameRank nameRank = list.get(i);
                                if(nameRank.getUserBaseInfo() == null){
                                    continue;
                                }
                                XRankProto.XUserRankMsg.Builder builder = ChannelEnterNameRankMgr.buildRankMsg(nameRank);
                                listMsg.addRank(builder.build());
                            }
                            clientMsg.setList(listMsg);
                        }

                        //我的排行
//                        if(page == 1) {
                            clientMsg.setMyRank(enterNameRankModel.getMyRank(userId, userEnterName.getServerId()));
                            EnterNameActivity nameActivity = ChannelEnterNameMgr.getEnterNameActivity(activityId);
                            if(nameActivity != null){
                                EnterNameMyRankData rankData = nameActivity.getMyRankData(type, userEnterName);
                                clientMsg.setExtendParam(rankData.getExtendParam());
                                clientMsg.setMyParam(rankData.getParam());
                                clientMsg.setMyValue(rankData.getValue());
//                                ChannelEnterNameRankMgr.getLogger().info("enter name activityId {}, userId {} my value {}, my param {}", activityId, userId, rankData.getValue(), rankData.getUserRankParam());
                            }
//                        }
                    }
                }else {

                }
            }else {
                ChannelEnterNameRankMgr.getLogger().info("activityId {}, user {}  UserEnterName null", activityId, reqMsg.getUserKey().getUserId());
            }
        }else {
            ChannelEnterNameRankMgr.getLogger().info("activityId  {}, user EnterNameModel null", activityId);
        }
        ChannelMessageHelper.sendGamePacket(Protocol.U_ENTER_NAME_USER_RANK, clientMsg, reqMsg.getUserKey().getChannelId(),
                reqMsg.getUserKey().getServerId(), reqMsg.getUserKey().getUserId());

    }

    @Override
    public void sendData() {

    }
}
